<!--
 * @Descripttion: 条码打印
 * @version: V1.0
 * @Author: GW
 * @Date: 2021-11-03 14:00:43
 * @LastEditors: GW
 * @LastEditTime: 2022-01-06 10:21:29
-->
<template>
    <div class="content-wrapper a-base-container">
        <div class="big-main-wrapper warehouse-type-wrapper requisition-set-wrapper" style="margin:2px 8px;">
            <div class="btn-wrapper clearfix">
                <a-row class="text-center clearfix">
                    <a-col :xs="24" :sm="24" :md="24" :lg="16" :xl="12">
                        <a-col :span="2" v-if="showModel=='order'">
                            <div class="btns" @click="changeView('list')">
                                <a-icon class="btns-icon" type="unordered-list" />
                                <div class="btns-text">打印记录</div>
                            </div>
                        </a-col>
                        <a-col :span="2" v-if="showModel=='list'">
                            <div class="btns" @click="changeView('order')">
                                <a-icon class="btns-icon" type="barcode" />
                                <div class="btns-text">打印条码</div>
                            </div>
                        </a-col>
                        <a-col :span="2" :class="showModel=='order'?'':'btns-disable'">
                            <div class="btns" @click="showModel=='order'?newAdd():''">
                                <a-icon class="btns-icon" type="file-add" />
                                <div class="btns-text">新增</div>
                            </div>
                        </a-col>
                        <!-- <a-col :span="2">
                            <div class="btns" @click="openOrderView">
                                <a-icon class="btns-icon" type="file" />
                                <div class="btns-text">选单</div>
                            </div>
                        </a-col> -->
                        <a-col :span="2" :class="showModel=='order'?'':'btns-disable'">
                            <div class="btns" @click="showModel=='order'?generateByline(3):''">
                                <a-icon class="btns-icon" type="save" />
                                <div class="btns-text">保存</div>
                            </div>
                        </a-col>
                        <a-col :span="2" :class="showModel=='list'?'':'btns-disable'">
                            <div class="btns" @click="showModel=='list'?showDetails():''">
                                <a-icon class="btns-icon" type="container" />
                                <div class="btns-text">查看详情</div>
                            </div>
                        </a-col>

                    </a-col>
                </a-row>
            </div>
            <div class="table-wrapper-box" v-show="showModel=='list'">
                <!-- :pagination="false" -->
                <a-table :row-selection="{ selectedRowKeys: selectedKeysList, onChange: onSelectChangeList }" :customRow="customClick" :columns="columns" :pagination="paginationList" :data-source="dataList" bordered />
            </div>
            <div class="order-warpper-box" v-show="showModel=='order'">
                <section>
                    <div><span class="audit-status" style="z-index:78" v-show="voucherState==1">已审</span></div>
                    <a-form-model :layout="formLayout">
                        <div class="header-box">
                            <a-row class="order-dynamic-form-wrapper">
                                <a-col :xs="grid.xs" :sm="grid.sm" :md="grid.md" :lg="grid.lg" :xl="grid.xl" :xxl="grid.xxl">
                                    <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="业务对象名称" :rules="[{ required: true, message:'',trigger:'input'}]">
                                        <a-select show-search @change="businessChange">
                                            <a-select-option v-for="(item,index) in businessObject" :key="item.voucherTypeCode" :value="item.voucherTypeName">
                                                {{item.voucherTypeName}}
                                            </a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :xs="grid.xs" :sm="grid.sm" :md="grid.md" :lg="grid.lg" :xl="grid.xl" :xxl="grid.xxl">
                                    <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="明细条码规则" :rules="[{ required: true, message:'',trigger:'input'}]">
                                        <a-select v-model="ruleId" @change="ruleChange">
                                            <a-select-option v-for="(item,index) in ruleJson" :dataItem="item" :key="index" :value="item.id">
                                                {{item.ruleName}}
                                            </a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :xs="grid.xs" :sm="grid.sm" :md="grid.md" :lg="grid.lg" :xl="grid.xl" :xxl="grid.xxl">
                                    <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="明细条码模板" :rules="[{ required: true, message:'',trigger:'input'}]">

                                        <a-input-group>
                                            <a-row :gutter="0">
                                                <a-col :span="24">
                                                    <a-select style="width:100%;" v-model="templateId" @change="templateChange">
                                                        <a-select-option v-for="(item,index) in templateJson" :dataItem="item" :key="index" :value="item.id">
                                                            {{item.name}}
                                                        </a-select-option>
                                                    </a-select>
                                                </a-col>
                                                <!-- <a-col :span="8">
                                                    <a-button @click="showTemplate">查看模板</a-button>
                                                </a-col> -->
                                            </a-row>
                                        </a-input-group>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :xs="grid.xs" :sm="grid.sm" :md="grid.md" :lg="grid.lg" :xl="grid.xl" :xxl="grid.xxl">
                                    <a-form-model-item :label-col="labelCol" :wrapper-col="wrapperCol" label="备注">
                                        <a-input v-model.trim="remark" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </div>
                    </a-form-model>
                </section>
                <div class="table-wrapper-box">
                    <div class="table-box-one">
                        <div class="table-title">源单明细</div>
                        <div class="table-button">
                            <a-button size="small" type="primary" @click="openOrderView" ghost>选取打印明细</a-button>
                            <a-button size="small" class="margin-left-10" @click="generateByline(2)">逐行生成</a-button>
                            <a-button size="small" class="margin-left-10" @click="generateByline(1)">合并生成</a-button>
                            <a-button size="small" class="margin-left-10" @click="clearDataSource">清空列表</a-button>
                            <!-- <a-button size="small" class="margin-left-10" @click="generateByline(3)">保存更新</a-button> -->
                        </div>
                        <a-table v-if="isRouterAlive" class="atable-edit" :columns="columnsDataSource" :data-source="dataSource" :pagination="false" bordered :scroll="{x:1000,y:384}">
                            <template v-for="item in ['expectedPrintQuantity','quantity','minPackageQuantity','remark']" :slot="item" slot-scope="text, record ,index">
                                <div>
                                    <editable-cell v-if="isRouterAlive" :record="record" :col="item" :text="text" @change="onCellChange(index, item, $event)" />
                                </div>
                            </template>
                        </a-table>
                    </div>

                    <div class="table-box-one">
                        <div class="table-title">条码明细</div>
                        <div class="table-button">
                            <a-button size="small" class="margin-left-10" @click="printPreviewLodop">打印</a-button>
                            <a-button size="small" class="margin-left-10" @click="showTemplate">查看模板</a-button>
                            <!-- <a-button size="small" class="margin-left-10" @click="printPreview">打印预览（弹窗）</a-button> -->
                            <a-button size="small" class="margin-left-10" @click="clearDataSourcePrint">清空列表</a-button>

                            <!-- <a-button size="small" class="margin-left-10" @click="printNow">直接打印</a-button> -->
                        </div>
                        <a-table v-if="isRouterAlive" class="atable-edit" :columns="columnsPrint" :data-source="dataSourcePrint" :pagination="false" bordered :scroll="{x:1000,y:384}">
                            <template v-for="item in ['quantity']" :slot="item" slot-scope="text, record ,index">
                                <div>
                                    <editable-cell v-if="isRouterAlive" :record="record" :col="item" :text="text" @change="onCellChange(index, item, $event)" />
                                </div>
                            </template>
                        </a-table>
                    </div>
                </div>
            </div>
        </div>

        <a-modal v-model="visible" :title="businessObjectName + '列表'" :width="1000">
            <div style="min-height:300px;">
                <a-table :data-source="datailsDataSource" :columns="datailColumns" :row-selection="{ selectedRowKeys: selectedKeys, onChange: onSelectChange,fixed:true, }" :pagination="paginationDetails" :scroll="{x:800,y:384}" bordered>
                    <template v-for="col in slotArray" :slot="col" slot-scope="text">
                        <!-- <div data-key="" v-if="text"> -->
                        <div data-key="">
                            {{text | NumFormat}}
                        </div>
                    </template>
                    <template slot="baseUnit" slot-scope="text">
                        {{text | unitFormart}}
                    </template>
                    <template slot="subUnit" slot-scope="text">
                        {{text | unitFormart}}
                    </template>
                    <template slot="sourceVoucherType" slot-scope="text">
                        {{conversionOrderName(text)}}
                    </template>

                </a-table>
            </div>
            <template slot="footer">
                <a-button key="back" @click="closeCheckOrder">
                    关闭
                </a-button>
                <a-button key="submit" type="primary" :loading="loading" @click="handleOk">
                    确认
                </a-button>
            </template>
        </a-modal>

        <!-- 打印预览 -->
        <a-modal title="打印预览" :visible="printView" :width="800" @cancel="()=>{this.printView = false;}">
            <div style="margin:35px auto;">
                <table cellpadding="10" border="1" style="font-family:幼圆;font-weight:bold;text-align:center;margin:0 auto;font-weight:bold;color:#000;border:1px solid #000;border-collapse: collapse;">
                    <tr>
                        <td colspan="4">重庆新西亚铝业(集团)股份有限公司</td>
                    </tr>
                    <tr>
                        <td style="width:100px">物料名称</td>
                        <td style="width:120px"></td>
                        <td style="width:20px">批次</td>
                        <td style="width:120px"></td>
                    </tr>
                    <tr>
                        <td>物料编码</td>
                        <td colspan="2"></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>规格型号</td>
                        <td colspan="2"></td>
                        <td rowspan="2"></td>
                    </tr>
                    <tr>
                        <td colspan="3" style="height:80px"></td>
                    </tr>
                </table>
            </div>
            <template slot="footer">
                <a-button type="primary" @click="()=>{this.printView = false;}">关闭</a-button>
            </template>
        </a-modal>

        <!-- 查看打印详情 -->
        <a-modal title="查看打印详情" :visible="printDetailsModal" :width="1000" @cancel="hideDetails">
            <div style="margin:35px auto;">
                <div style="margin-bottom:10px;">
                    <a-button size="small" @click="printDetailsAgain">再次打印</a-button>
                </div>
                <a-table :columns="columnsPrint" :data-source="dataSourceShowDetails" :row-selection="{ selectedRowKeys: selectedAgainKeys, onChange: onSelectAgainChange}" :pagination="false" bordered :scroll="{x:1000,y:384}">

                </a-table>
            </div>
            <template slot="footer">
                <a-button type="primary" @click="hideDetails">关闭</a-button>
            </template>
        </a-modal>
        <!--是否安装打印插件-->
        <barcodeUtil ref="printPlugIn"></barcodeUtil>
    </div>
</template>
<script>

import { mapState } from "vuex";
import editableCell from "./editTable/EditableCell"
import { getLodop } from "@/utils/tool/LodopFun"
import { listRule, listTemplate, getVoucherDesign, generateTheBarcode, barcodeDetail, pageList } from "@/api/inventory/barcode";
import { IdFieldToNameField } from "@/utils/customize"
import { systemplan, detailsList } from '@/api/erp/reportProgramme';
import { getAction, postAction } from "@/api/manage";
import barcodeUtil from './barcodeUtil';
import { conversionOrderName } from "@/utils/tool/parametric-switch"
let units = [];//计量单位所有数据
var LODOP;
export default {
    components: {
        editableCell,
        barcodeUtil
    },
    data () {
        return {
            showModel: 'list',
            //----------↓样式----------
            fromStyle: { paddingTop: "8px" },
            labelCol: { span: 10 },
            wrapperCol: { span: 14 },

            grid: {
                xs: 24,
                sm: 24,
                md: 12,
                lg: 8,
                xl: 8,
                xxl: 6
            },
            btn_grid: {
                xs: 2,
                sm: 2,
                md: 2,
                lg: 2,
                xl: 2
            },
            //----------↑样式----------
            isRouterAlive: true,
            columnsDataSource: [{
                title: '序号', dataIndex: 'index', width: 40, align: 'center',
                customRender: (text, record, index) => {
                    return index + 1;
                }
            }, {
                title: '预计打印数量', dataIndex: 'expectedPrintQuantity', scopedSlots: { customRender: 'expectedPrintQuantity' }, width: 80,
            }, {
                title: '数量', dataIndex: 'quantity', scopedSlots: { customRender: 'quantity' }, width: 80,
            }, {
                title: '最小包装数量', dataIndex: 'minPackageQuantity', scopedSlots: { customRender: 'minPackageQuantity' }, width: 80,
            }, {
                title: '物料编码', dataIndex: 'inventoryCode', width: 80,
            }, {
                title: '物料名称', dataIndex: 'inventoryName', width: 80,
            }, {
                title: '规格型号', dataIndex: 'inventorySpecification', width: 80,
            }, {
                title: '编号', dataIndex: 'no', width: 80,
            }, {
                title: '单个面积', dataIndex: 'unitArea', width: 80,
            }, {
                title: '颜色名称', dataIndex: 'colorName;', width: 80,
            }, {
                title: '条码规则名称', dataIndex: 'barcodeRuleName', width: 80,
            }, {
                title: '条码规则编码', dataIndex: 'barcodeRuleCode', width: 80,
            }, {
                title: '来源单号', dataIndex: 'sourceVoucherCode', width: 80,
            }, {
                title: '来源单据', dataIndex: 'sourceVoucherType', width: 80,
            }, {
                title: '备注', dataIndex: 'remark', width: 80,
            },],
            dataSource: [],//源单数据
            // { code: 'XXY1-01', batch: '1', number: 'P1面高幢12', process: '1/8', barcode: '215689654871', quantity: 1, },
            // { code: 'XXY1-02', batch: '1', number: 'P1面高幢12', process: '2/8', barcode: '548512012302', quantity: 1, },
            // { code: 'XXY1-03', batch: '1', number: 'P1面高幢12', process: '3/8', barcode: '452102130212', quantity: 1, },
            // { code: 'XXY1-04', batch: '1', number: 'P1面高幢12', process: '4/8', barcode: '231241201242', quantity: 1, },
            // { code: 'XXY1-05', batch: '1', number: 'P1面高幢12', process: '5/8', barcode: '120512032121', quantity: 1, },

            columnsPrint: [{
                title: '序号', dataIndex: 'index', width: 40, align: 'center',
                customRender: (text, record, index) => {
                    return index + 1;
                }
            }, {
                title: '条码', dataIndex: 'barCode', width: 240,
            }, {
                title: '打印数量', dataIndex: 'printQuantity', width: 100,
            }, {
                title: '打印模板名称', dataIndex: 'printTemplateName', width: 140,
            }, {
                title: '打印模板编码', dataIndex: 'printTemplateCode', width: 140,
            }, {
                title: '累积打印次数', dataIndex: 'cumPrintQuantity', width: 100,
            }, {
                title: '物料编码', dataIndex: 'inventoryCode', width: 140,
            }, {
                title: '物料名称', dataIndex: 'inventoryName', width: 140,
            }, {
                title: '规格型号', dataIndex: 'inventorySpecification', width: 100,
            }, {
                title: '数量', dataIndex: 'quantity', width: 100,
            }, {
                title: '条码规则名称', dataIndex: 'barcodeRuleName', width: 140,
            }, {
                title: '条码规则编码', dataIndex: 'barcodeRuleCode', width: 140,
            }],
            dataSourcePrint: [],
            columns: [{
                title: '业务对象名称', dataIndex: 'voucherTypeCode', customRender: (text, record, index) => {
                    return this.conversionOrderName(text)
                }
            }, {
                title: '条码规则名称', dataIndex: 'barcodeRuleName'
            }, {
                title: '条码规则编码', dataIndex: 'barcodeRuleCode'
            }, {
                title: '打印模板名称', dataIndex: 'printTemplateName'
            }, {
                title: '打印模板编码', dataIndex: 'printTemplateCode'
            }, {
                title: '条码打印时间', dataIndex: 'createDate'
            }],
            visible: false,
            businessObject: null,       //业务对象json
            businessObjectName: null,    //业务对象名称
            businessObjectCode: null,
            businessRule: null,         //明细条码规则
            businessTemplate: null,     //明细条码模板
            selectedKeys: [],
            selectedRows: [],
            reportCode: null,        //获取单据详情code
            backData: [],           //动态搜索表单数据
            datailsDataSource: [],      //详情数据
            datailColumns: [],      //弹窗表单数据详情的columns
            slotArray: [],          //小数或者整数字段集合

            ruleJson: [],            //条码规则json
            ruleId: null,
            ruleCode: null,
            ruleName: null,
            templateJson: [],         //模板json
            templateId: null,
            templateCode: null,
            templateName: null,
            templateContent: null,
            remark: null,
            backId: null,//返回
            printView: false,//打印预览
            sourceCode: null,//当前选择的业务对象code
            submitRequestData: {},
            dataList: [],//列表数据
            selectedKeysList: [],
            selectedRowsList: [],
            printData: [],//单条列表详细数据
            showModal: 'list',
            customClick: (record, index) => ({
                on: {
                    dbclick: () => {
                        this.showModel = 'roder';
                        this.printData = record;
                    }
                },
            }),
            printDetailsModal: false,
            paginationList: {
                size: "small",
                current: 1,
                pageSize: 20,
                total: 0,
                onChange: (cur) => {
                    this.paginationList.current = cur;
                    this.pageList();
                }
            },
            paginationDetails: {
                size: "small",
                current: 1,
                pageSize: 20,
                total: 0,
                onChange: (cur) => {
                    this.paginationDetails.current = cur;
                    this.handlerSearch();
                }
            },

            selectedAgainKeys: [],//列表查看再次打印
            selectedAgainRows: [],//列表查看再次打印
        };
    },
    created () {
        this.getVoucherDesign();
        this.getUnit();//获取计量单位
        this.getRule();//获取规则
        this.getTemplate();//获取模板
        this.pageList();//获取打印列表
    },
    computed: {
        ...mapState({
            userInfo: state => state.user.info,
            permissions: state => state.user.permissions,
        }),
    },
    methods: {
        conversionOrderName,
        reload () {
            //强制刷新表格
            this.isRouterAlive = false;
            this.$nextTick(function () {
                this.isRouterAlive = true;
            });
        },
        newAdd () {
            //新增
            this.showModel = 'order';
            this.datailsDataSource = [];
            this.selectedKeysList = [];
            this.selectedRowsList = [];
            this.dataSourcePrint = [];
            this.businessObjectName = null;    //业务对象名称
            this.businessObjectCode = null;
            this.businessRule = null;         //明细条码规则
            this.businessTemplate = null;     //明细条码模板
            this.selectedKeys = [];
            this.selectedRows = [];
            this.reportCode = null;        //获取单据详情code
            this.backData = [];           //动态搜索表单数据
            this.datailsDataSource = [];      //详情数据
            this.datailColumns = [];      //弹窗表单数据详情的columns
            this.slotArray = [];          //小数或者整数字段集合
            this.ruleId = null;
            this.ruleCode = null;
            this.ruleName = null;
            this.templateId = null;
            this.templateCode = null;
            this.templateName = null;
        },
        changeView (view) {
            this.showModel = view;
            if (view == 'list') {
                this.pageList();
            }
        },
        openOrderView () {
            //选单
            if (this.businessObjectCode == null) {
                this.$message.warning('请先选择业务对象');
                return;
            }

            if (this.ruleName == null) {
                this.$message.warning('请选择条码规则');
                return;
            }

            if (this.templateName == null) {
                this.$message.warning('请选择条码模板');
                return;
            }

            this.visible = true;
        },
        //查看模板
        showTemplate () {
            if (this.$refs.printPlugIn.CheckIsInstall()) {
                //是否下载打印插件
                return;
            }
            if (this.templateId == null) {
                this.$message.warning('请先选择明细条码模板');
                return;
            };
            let that = this;
            LODOP = getLodop();
            eval(that.templateContent);
            LODOP.SET_SHOW_MODE("HIDE_PAPER_BOARD", 1);
            LODOP.SET_PREVIEW_WINDOW(0, 3, 0, 1200, 800, "");
            LODOP.PREVIEW();
        },
        printPreview () {
            //打印预览
            if (this.dataSourcePrint.length < 1) {
                this.$message.warning('请选择需要打印的数据');
                return;
            }
            this.printView = true;
        },
        printPreviewLodop () {
            //打印预览Lodop
            console.log(this.dataSourcePrint);
            if (this.dataSourcePrint.length < 1) {
                this.$message.warning('请选择需要打印的数据');
                return;
            }
            LODOP = getLodop();
            LODOP.PRINT_INIT("");
            this.nihao(this.dataSourcePrint);
            LODOP.PREVIEW();
        },
        printDetailsAgain () {
            if (this.$refs.printPlugIn.CheckIsInstall()) {
                //是否下载打印插件
                return;
            }
            //预览再次打印
            if (this.selectedAgainKeys.length < 1) {
                this.$message.warning('请选择需要再次打印的数据');
                return;
            }
            LODOP = getLodop();
            LODOP.PRINT_INIT("");
            this.nihao(this.selectedAgainRows);
            LODOP.PREVIEW();
        },
        nihao (choiceData) {
            for (let i = 0; i < choiceData.length; i++) {
                let data = JSON.parse(choiceData[i].extendJson);
                LODOP.NewPage();

                LODOP.ADD_PRINT_TABLE(13, 0, "100.28mm", "75.67mm", "<table  border=\"1\"  cellpadding=\"10\" style=\"font-family:幼圆;font-weight:bold;text-align:center;margin:0 auto;font-weight:bold;color:#000;border:1px solid #000;border-collapse: collapse;\">\r\n                    <tr>\r\n                        <td colspan=\"4\">重庆新西亚铝业(集团)股份有限公司</td>\r\n                    </tr>\r\n                    <tr>\r\n                        <td style=\"width:40px\">单号</td>\r\n                        <td style=\"width:120px\"></td>\r\n                        <td style=\"width:20px\">批次</td>\r\n                        <td style=\"width:80px\"></td>\r\n                    </tr>\r\n                    <tr>\r\n                        <td>编号</td>\r\n                        <td colspan=\"2\"></td>\r\n                        <td></td>\r\n                    </tr>\r\n                    <tr>\r\n                        <td>颜色</td>\r\n                        <td colspan=\"2\"></td>\r\n                        <td rowspan=\"2\"></td>\r\n                    </tr>\r\n                    <tr>\r\n                        <td colspan=\"3\" style=\"height:80px\"></td>\r\n                    </tr>\r\n                </table>");
                LODOP.ADD_PRINT_TEXT(69, 82, 125, 32, data.projectName);//项目名称
                LODOP.SET_PRINT_STYLEA(0, "FontName", "幼圆");

                if (data.projectName.length >= 7) {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
                } else if (data.projectName.length >= 10) {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
                } else {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                }

                LODOP.ADD_PRINT_TEXT(115, 82, 170, 32, data.no);//编号

                LODOP.SET_PRINT_STYLEA(0, "FontName", "幼圆");
                if (data.no.length >= 14) {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
                } else if (data.no.length >= 18) {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
                } else {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                }

                LODOP.ADD_PRINT_TEXT(154, 82, 172, 32, data.colorName);//颜色
                LODOP.SET_PRINT_STYLEA(0, "FontName", "幼圆");

                if (data.colorName.length >= 14) {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 10);
                } else if (data.colorName.length >= 18) {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 8);
                } else {
                    LODOP.SET_PRINT_STYLEA(0, "FontSize", 12);
                }

                LODOP.ADD_PRINT_TEXT(61, 267, 86, 47, data.batch);//批次
                LODOP.SET_PRINT_STYLEA(0, "FontName", "幼圆");
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 21);
                LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
                LODOP.ADD_PRINT_TEXT(114, 267, 86, 32, choiceData[i].id);//id
                LODOP.SET_PRINT_STYLEA(0, "FontName", "幼圆");
                LODOP.SET_PRINT_STYLEA(0, "FontSize", 14);
                LODOP.SET_PRINT_STYLEA(0, "Alignment", 2);
                LODOP.ADD_PRINT_BARCODE(199, 28, 226, 60, "128A", choiceData[i].barCode);//条形码
                LODOP.ADD_PRINT_BARCODE(172, 270, 106, 112, "QRCode", choiceData[i].barCode);//二维码
            }
        },
        printNow () {
            //直接打印
            if (this.dataSourcePrint.length < 1) {
                this.$message.warning('请选择需要打印的数据');
                return;
            }
            LODOP = getLodop();
            LODOP.PRINT_INIT("");
            this.nihao(this.dataSourcePrint);
            LODOP.PRINT();
        },
        onSelectChange (keys, rows) {
            this.selectedKeys = keys;
            this.selectedRows = rows;
        },
        //关闭选单
        closeCheckOrder () {
            this.visible = false;
        },
        //确定选单
        handleOk () {
            if (this.selectedKeys.length < 1) {
                this.$message.warning('请选择数据');
                return;
            }
            this.visible = false;
            // console.log(this.selectedRows);
            let data = [];
            this.selectedRows.forEach((item, index) => {
                let d = _.cloneDeep(item);
                let itemTemp = {};
                for (const key in d) {
                    if (key.indexOf('_') != -1) {
                        itemTemp[key.split('_')[1]] = d[key];
                    } else {
                        itemTemp[key] = d[key];
                    }
                }
                data[index] = itemTemp;
            });
            data.forEach(item => {
                item.createDate = null;
                item.reviewerDate = null;
                if (item.quantity) {
                    item.expectedPrintQuantity = item.quantity;//预计打印数量
                } else {
                    item.quantity = item.baseQuantity;//数量
                    item.expectedPrintQuantity = item.baseQuantity;//预计打印数量
                }

                item.barcodeRuleName = this.ruleName
                item.barcodeRuleCode = this.ruleCode;
                item.sourceDetailId = item.id;

                item.barcodeRuleName = this.ruleName;   //条码规则名称
                item.barcodeRuleCode = this.ruleCode;   //条码规则编码
                item.printTemplateName = this.templateName; //条码模板名称
                item.printTemplateCode = this.templateCode; //条码模板编码

            });
            console.log('源数据：', data)
            this.dataSource = data;
            this.reload();
        },
        //获取业务对象
        getVoucherDesign () {
            getVoucherDesign().then(res => {
                if (res.code === 200) {
                    res.data.forEach(e => {
                        e.key = e.id;
                    });
                    this.businessObject = res.data;
                }
            })
        },
        //获取条码规则
        getRule () {
            let params = { size: 200, current: 1 };
            listRule(params).then(res => {
                if (res.code == 200) {
                    this.ruleJson = res.data.records;
                    if (JSON.stringify(res.data.records) != '[]') {
                        if (res.data.records.length > 0) {
                            this.ruleJson = res.data.records.map(r => {
                                return { ...r, key: r.id }
                            })
                        }
                    }
                } else {
                    // this.$message.warning(res.message);
                }
            })
        },
        //获取模板
        getTemplate () {
            let params = { size: 200, current: 1 };
            listTemplate(params).then(res => {
                if (res.code == 200) {
                    this.templateJson = res.data;
                    if (JSON.stringify(res.data) != '[]') {
                        if (res.data.length > 0) {
                            this.templateJson = res.data.map(r => {
                                return { ...r, key: r.id }
                            })
                        }
                    }
                } else {
                    // this.$message.warning(res.message);
                }
            })
        },
        //模板数据赋值
        templateChange (key, row) {
            console.log(key, row.data.attrs.dataItem);
            let data = row.data.attrs.dataItem;
            this.templateContent = data.content;
            this.templateCode = data.code;
            this.templateName = data.name;
        },
        //规则数据赋值
        ruleChange (key, row) {
            console.log(row.data.attrs.dataItem)
            let data = row.data.attrs.dataItem;
            this.ruleCode = data.ruleCode;
            this.ruleName = data.ruleName;
        },
        //获取系统方案
        getSystemplan () {
            systemplan({ reportCode: this.reportCode }).then((res) => {
                if (res.code == 200) {
                    this.backData = res.data;
                    this.handleJoinCloumns(res.data);
                    this.handlerSearch();
                }
            }).catch((e) => {
                // this.$message.error('请求出错', 1)
            })
        },
        //业务对象改变
        businessChange (key, row) {
            console.log(key, row);
            this.businessObjectCode = row.data.key;
            this.businessObjectName = row.componentOptions.children[0].text;
            this.reportCode = row.data.key + '_Detail_Report';
            this.sourceCode = row.data.key;
            this.getSystemplan();
        },
        //获取明细列表数据
        handlerSearch () {
            let baseRequestData = {
                dataSource: this.sourceCode,
                erpReportDesignList: this.backData,
                page: {
                    'current': this.paginationDetails.current,
                    'pages': this.paginationDetails.pageSize,
                    'size': this.paginationDetails.pageSize - 1,
                },
                someBusinessType: null
            }
            detailsList(baseRequestData).then((res) => {
                if (res.code == 200) {
                    this.datailsDataSource = res.data.records.map(v => {
                        return { ...v, key: v.id }
                    });
                    this.paginationDetails.current = res.data.current;
                    this.paginationDetails.total = res.data.total;
                    this.$forceUpdate();//强制更新
                }
            }).catch((e) => {
                // this.$message.error('请求出错', 1)
            })
        },
        onCellChange (indexTable, dataIndex, value) {
            const dataSource = [...this.dataSource];
            const target = dataSource.find((item, index) => index === indexTable);
            console.log(target, dataIndex, value, indexTable);

            this.$set(target, dataIndex, value)
            this.$nextTick(() => {
                this.dataSource = dataSource;
                console.log('明细数据：', this.dataSource)
                this.reload();
            })

        },
        //条码打印
        generateByline (type) {
            //保存 方式 ：1：合并生单  2：逐行生单 3：保存更新
            if (this.dataSource.length < 1) {
                this.$message.warning('请选择数据');
                return;
            }
            let requestData = {
                barcodeRuleId: this.ruleId,//条码规则ID
                barcodeRuleCode: this.ruleCode,//条码规则编码
                barcodeRuleName: this.ruleName,//条码规则名称
                printTemplateCode: this.templateCode,//打印模板编码
                printTemplateId: this.templateId, //打印模板ID
                printTemplateName: this.templateName,//打印模板名称
                voucherTypeCode: this.businessObjectCode,//单据类型唯一编码（业务对象）
                remark: this.remark,
                printSources: this.dataSource,//源单明细
                actionType: type,

                sourceVoucherType: this.businessObjectCode,//来源单
            }
            generateTheBarcode(requestData).then(res => {
                if (res.code === 200) {
                    console.log(res.data);
                    //打印id
                    this.backId = res.data;
                    this.getBarcodeDetail();
                }
            })
        },
        //获取条码打印详情
        getBarcodeDetail () {
            barcodeDetail({ id: this.backId }).then(res => {
                if (res.code === 200) {
                    console.log(res.data);
                    this.submitRequestData = res.data;
                    this.dataSourcePrint = res.data.printDetails;
                }
            })
        },
        //查看打印详情
        showDetails () {
            if (this.selectedKeysList.length != 1) {
                this.$message.warning('请选择一条打印记录查看');
                return;
            }
            barcodeDetail({ id: this.selectedKeysList[0] }).then(res => {
                if (res.code === 200) {
                    this.printDetailsModal = true;
                    this.dataSourceShowDetails = res.data.printDetails;
                }
            })
        },
        //关闭打印详情
        hideDetails () {
            this.printDetailsModal = false;
            this.selectedAgainRows = [];
            this.selectedAgainKeys = [];
        },
        onSelectAgainChange (key, row) {
            this.selectedAgainKeys = key;
            this.selectedAgainRows = row;
        },
        //组装cloumns
        handleJoinCloumns (data) {
            let list = data;
            this.datailColumns = [];
            list.forEach(detail => {

                let codeA = detail.code;
                if (detail.columnType == '搜索选择' || detail.columnType == '模糊搜索') {
                    codeA = IdFieldToNameField(codeA)
                }
                let s = detail.tableName.trim();
                let code = s.substring(s.lastIndexOf(" ") + 1) + '_' + codeA;
                // console.log("列", code)
                // let code = detail.tableName.split(" ")[1] + '_' + codeA;

                let textAlign = 'left';
                if (detail.columnType == '小数' || detail.columnType == '整数') {
                    textAlign = 'right';
                }

                let cloumns = {
                    title: detail.name,
                    dataIndex: code,
                    key: code,
                    width: detail.columnWidth,
                    ellipsis: true,
                    align: textAlign,
                    customCell: (record, rowIndex) => {		//在此处定义
                        if (record.isSum == true) {
                            return { style: { 'color': '#222', 'font-size': '12px', 'font-weight': 'bold' } }    //return 想要设置的样式
                        }
                    }
                }

                //业务类型 1.普通采购 2.固定资产 3.普通销售 96:销售退货(冲抵) 97: 采购退货(冲抵) 98.销售退货 99.采购退货
                // if (detail.code == 'businessType') {
                //     cloumns.scopedSlots = { customRender: 'businessType' }
                // }

                if (detail.columnType == '下拉选择' && detail.val) {
                    let valList = [];
                    try {
                        valList = JSON.parse(detail.val);
                    } catch (error) {
                        return "";
                    }
                    cloumns.customRender = (text, row, index) => {
                        let findOne = valList.find(f => f.key == text);
                        return findOne ? findOne.label : "";
                    }
                }

                if (detail.columnType == '小数' || detail.columnType == '整数') {
                    this.slotArray.push(detail.code);
                    cloumns.scopedSlots = { customRender: detail.code }
                }

                if (detail.code == 'baseUnit') {//主计量
                    cloumns.scopedSlots = { customRender: 'baseUnit' }
                }
                if (detail.code == 'subUnit') {//辅助计量
                    cloumns.scopedSlots = { customRender: 'subUnit' }
                }

                if (detail.code == 'sourceVoucherType') {//来源单据
                    cloumns.scopedSlots = { customRender: 'sourceVoucherType' }
                }

                if (detail.onShow) {
                    //明细
                    this.datailColumns.push(cloumns);
                }
            });
            this.datailColumns.unshift({
                title: '序号',
                dataIndex: 'number',
                width: 40,
                align: 'center',
                customRender: (text, record, index) => {
                    return index + 1;
                }
            })
            console.log('columns:', this.datailColumns)
            this.$forceUpdate();
        },
        //获取计量单位
        getUnit () {
            let postData = {
                current: 1,
                size: 500,
                unitGroupId: '',
            }
            getAction('/in/unit/pageList', postData).then(response => {
                if (response.code === 200) {
                    console.log(response);
                    units = response.data.records;
                }
            }).catch(error => { });
        },
        clearDataSource () {
            //清空源单明细
            this.dataSource = [];
            this.reload();
        },
        clearDataSourcePrint () {
            //清空条码明细
            this.dataSourcePrint = [];
            this.reload();
        },
        pageList () {
            let requestData = {
                size: this.paginationList.pageSize,
                current: this.paginationList.current,
            }
            pageList(requestData).then(res => {
                if (res.code === 200) {
                    if (res.data.records.length > 0) {
                        res.data.records.forEach(e => {
                            e.key = e.id;
                        });
                        this.dataList = res.data.records;
                        this.paginationList.current = res.data.current;
                        this.paginationList.size = res.data.size;
                        this.paginationList.total = res.data.total;
                    } else {
                        this.dataList = [];
                    }

                }
            })
        },
        onSelectChangeList (keys, rows) {
            this.selectedKeysList = keys;
            this.selectedRowsList = rows;
        },
    },
    filters: {
        unitFormart (id) {
            let act = '';
            if (units.length > 0) {
                for (let i = 0; i < units.length; i++) {
                    if (units[i].id == id) {
                        act = units[i].name;
                    }
                }
            }
            return act;
        }
    }
};
</script>
<style lang="less">
    // @import '~@/style/setting/inventory.less';
    @import '~@/style/purchase/voucher.less';
    .table-wrapper-box {
        padding: 10px;
    }
    .order-warpper-box {
        overflow-y: auto;
        padding: 10px;
        height: calc(100% - 45px);
        .table-box-one {
            margin-bottom: 20px;
            min-height: 300px;
            .table-title {
                font-weight: bold;
            }
            background: #eee;
            padding: 10px;
        }
        .table-button {
            height: 32px;
        }
    }
    .barcode-box {
        .ant-form-item {
            margin-bottom: 0px;
        }
    }
</style>